package com.work.service.common;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.common.dto.CargoCategoryDTO;
import com.common.feign.ICommonServeClient;
import com.common.vo.CargoCategoryVO;
import com.common.vo.CargoLocationDict;
import com.common.vo.CargoLocationInfoVO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.entity.DictBiz;
import org.springblade.system.user.DTO.UserDeptBizDTO;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.feign.IUserClient;
import org.springblade.system.user.vo.UserVO;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 通用服务实现类
 *
 * @author ：dujianyu
 * @since ：Created in 2022/6/15 8:48
 */
@AllArgsConstructor
@Service
@DS("master_tjpn4")
public class CommonServiceImpl implements ICommonService {
    private ICommonServeClient commonServeClient;
    private IUserClient userClient;

    @Override
    public String userName(String ids) {
        List<User> user = userClient.userInfoByIds(ids).getData();
        List<String> userNames = user.stream().map(User::getName).collect(Collectors.toList());
        return String.join(",", userNames);
    }

    @Override
    public Map<Long, String> cargoMap() {
        List<CargoCategoryVO> list = commonServeClient.list(new CargoCategoryDTO()).getData();
        return list.stream().collect(Collectors.toMap(CargoCategoryVO::getId, CargoCategoryVO::getName, (key1, key2) -> key1 ));
    }

    @Override
    public Map<String, Long> cargoCodeIdMap() {
        List<CargoCategoryVO> list = commonServeClient.list(new CargoCategoryDTO()).getData();
        return list.stream().collect(Collectors.toMap(CargoCategoryVO::getCode, CargoCategoryVO::getId, (key1, key2) -> key1 ));
    }

    @Override
    public Map<Long, String> positionMap() {
        CargoLocationDict data = commonServeClient.cargoLoactionR(null).getData();
        return data.getList().stream().collect(Collectors.toMap(CargoLocationInfoVO::getId, CargoLocationInfoVO::getName,  (key1, key2) -> key1 ));
    }

    @Override
    public Map<Long, Long> positionArea() {
        CargoLocationDict data = commonServeClient.cargoLoactionR(null).getData();
        return data.getList().stream().collect(Collectors.toMap(CargoLocationInfoVO::getId, CargoLocationInfoVO::getAreaId,  (key1, key2) -> key1 ));
    }


    public Map<String, String> areaMap() {
        return commonServeClient.areaSettingsMap();
    }
    @Override
    public List<UserVO> getUsers() {
        UserDeptBizDTO dto = new UserDeptBizDTO();
        dto.setUserId(AuthUtil.getUserId());
        dto.setSubsystemCode("tally");
        dto.setCode("tally");
        return userClient.getUsersList(dto).getData();
    }

    @Override
    public String holdOrBerthNames(String holdOrBerth, List<DictBiz> dictBizList) {
        if (!Func.isBlank(holdOrBerth)) {
            List<String> valueList = new ArrayList<>();
            String[] holdStr = holdOrBerth.split(",");
            for (String s : holdStr) {
                for (DictBiz dictBiz : dictBizList) {
                    if (Objects.equals(dictBiz.getDictKey(), s)) {
                        valueList.add(dictBiz.getDictValue());
                    }
                }
            }
            return StringUtils.join(valueList, ",");
        } else {
            return "";
        }
    }


    /**判断是否超过多少小时 如：24

     *

     * @param tableTime 业务时间

     * @param hour 多少小时

     * @return boolean

     * @throws Exception

     */

    @Override
    public boolean judgmentDate(String tableTime, Integer hour) throws Exception {
        /*
         * 当前时间
         */
        String currentTime = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        /*
         * 业务时间
         */
        Date start = sdf.parse(tableTime);

        /*
         * 当前时间
         */
        Date end = sdf.parse(currentTime);

        long cha = end.getTime() - start.getTime();

        if (cha < 0) {

            return false;

        }

        double result = cha * 1.0 / (1000 * 60 * 60);

        /*
         * 是小于等于 hour 小时
         */
        return result <= hour;

    }
}
